// packageC/pages/coups/toSignUp.js
var common = require("../../../public.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        checked: false,
        userInfo: {},
        show: false,
        showCheng: false,
        textList: [],
        paramList: [], //后台需要数组
        showInfo: {},
        twoTime: '',
        timeData: {},
        enrollPromise: '',
        activId: '',
        imgUrl:''
    },
    // 查看中奖名单
    goWinPrize() {

    },

    // 活动介绍
    activeFn() {
        const that = this
        let temp = {
            activId: this.data.activId,
            isOnline: this.data.isOnline
        }
        common.post('/activExplain/content', temp, function (res) {
            if (res.status === 200) {
                console.log(res.data, "54554774414144")
                that.setData({
                    details: res.data
                })
            } else {
                common.showToast(res.msg)
            }
        })
    },
    onChangesss(e) {
        this.setData({
            timeData: e.detail,
        });
    },
    // 我知道了
    click_know() {
        var that = this
        this.setData({
            showCheng: false
        })
    },
    oninvoiceCode(e) {
        // console.log(e)
        let {
            item,
            index
        } = e.currentTarget.dataset
        let content = e.detail
        // console.log(index, item, item.id, content)
        // console.log(this.data.paramList[])
        this.data.paramList[index] = {
            "couponExamId": item.id,
            "couponExamVal": content
        }
        // console.log(this.data.paramList)
    },
    onClickShow() {
        this.setData({
            show: true
        });
    },

    onClickHide() {
        this.setData({
            show: false
        });
        // setTimeout(()=>{
        wx.navigateTo({
            url: `/packageC/pages/coups/signUp&imgUrl=${this.data.imgUrl}`,
        })
        // },1000)
    },
    click_close() {
        this.setData({
            showCheng: false
        });
    },
    noop() { },
    onChange(event) {
        this.setData({
            checked: event.detail,
        });
    },
    click_cheng() {
        this.setData({
            showCheng: true
        })
    },

    // 是否实名
    isReal() {
        const that = this
        console.log("5566644747411")
        let temp = {
            userId: that.data.userInfo.id,
        }
        common.post('/adminCouponCon/apps/identityInfo', temp, function (res) {
            if (res.status === 200) {
                //   console.log(res,"resresresres")
            } else {
                common.showToast(res.msg)
            }
        })
    },
    // 确认提交
    goSign() {
        var that = this
        if (!that.data.checked) {
            common.showToast('请阅读报名协议户')
            return
        }
        // 冗余待优化
        let arrs = []
        var nameIndex = this.data.textList.findIndex((item) => item.examName == '姓名')

        let idsIndex = ''
        for (var i = 0; i < this.data.textList.length; i++) {
            var item = this.data.textList[i]
            console.log(item.examName)
            if (item.examName === '身份证' || item.examName == '身份证号') {
                console.log(i,"554444444444444444444")
                idsIndex = i
            }
        }
        console.log( idsIndex," idsIndex idsIndex")
        let phoneIndex = ''
        for (var i = 0; i < this.data.textList.length; i++) {
            var item = this.data.textList[i]
            if (item.examName == '手机号' || item.examName == '手机号码') {
                phoneIndex = i
            }
        }
        this.data.paramList[nameIndex] = { 'couponExamId': this.data.textList[nameIndex].id, couponExamVal: this.data.userInfo.realName }
        this.data.paramList[idsIndex] = { 'couponExamId': this.data.textList[idsIndex].id, couponExamVal: this.data.userInfo.ids }
        this.data.paramList[phoneIndex] = { 'couponExamId': this.data.textList[phoneIndex].id, couponExamVal: this.data.userInfo.phone }

        //比较长度是否必填项
        let newArr = that.data.textList.filter(item => item.isRequire == 1)
        console.log(newArr)
        console.log(this.data.paramList.length)
        if (this.data.paramList.length != newArr.length) {
            common.showToast('必填项不能为空')
            return
        }
        var obj = {
            'Content-Type': {
                "activId": that.data.activId,
                "userId": that.data.userInfo.id,
                "userName": that.data.userInfo.realName,
                "idCard": that.data.userInfo.ids,
                "phone": that.data.userInfo.phone,
                "paramList": this.data.paramList,
            }
        }
        common.postJson('/adminCouponCon/apps/bookingAdminCoupon', obj['Content-Type'], function (res) {
            if (res.status === 200) {
                console.log(res.data, "11111")
                if (res.data) {
                    that.setData({
                        showInfo: res.data
                    }, () => {
                        that.setData({
                            show: true
                        })
                        setTimeout(() => {
                            wx.navigateTo({
                                url: `/packageC/pages/coups/signUp?isOnline=${that.data.isOnline}&activId=${that.data.activId}&imgUrl=${that.data.imgUrl}`,
                            })
                        }, 1000)
                    })

                }

            } else {
                common.showRequestErrMsg(res)
                if (res.msg == '报名身份证号为空') {
                    common.toRealName()
                }
                // setTimeout(() => {
                //     wx.navigateTo({
                //         url: '/packageC/pages/coups/signUp?id=1',
                //     })
                // }, 1000)
            }
        })


    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this
        common.getLocalUserInfo(function (success, data) {
            if (success) {
                console.log(data, "datadatadatadata")
                that.setData({
                    userInfo: data.user,
                    token: data.token,
                    activId: options.activId || 50,
                    imgUrl: options.imgUrl ? options.imgUrl : '',
                    isOnline: options.isOnline ? options.isOnline : '',

                    // twoTime: Number(options.twoTime)
                }, function () {
                    // that.coupList()
                    that.coupFn()
                    that.activeFn()
                    that.StatusFn()
                    that.isReal()
                })
            } else {
                that.setData({
                    userInfo: null,
                    token: '',
                })
            }
        }, true)
    },

    // 检查状态
    StatusFn() {
        let that = this
        let temp = {
            activId: that.data.activId,
            userId: that.data.userInfo.id,
        }
        common.post('/adminCouponCon/apps/getMyBookCouponStatus', temp, function (res) {
            if (res.status === 200) {
                console.log(common.dateFormat('yyyy-MM-dd HH:mm:ss', res.data.lotteryTime), "小时候小时候")
                // enrollBeginTime开始报名时间，截止倒计时
                const nowDate = new Date().getTime(); //当前时间
                let {
                    enrollBeginTime,
                    enrollEndTime,
                    lotteryTime,
                    status
                } = res.data
                if (((nowDate - enrollEndTime) / 1000) >= 0) {
                    console.log("活动截止")

                } else {
                    console.log("活动报名开始")
                    let twoTime = enrollEndTime - nowDate //活动未开始的时间倒计时
                    that.setData({
                        twoTime
                    })
                }
                that.setData({
                    objss: res.data,
                    statusFn: res.data.status,
                    enrollBeginTime: common.dateFormat('yyyy-MM-dd HH:mm:ss', res.data.enrollBeginTime),
                    enrollPromise: res.data.enrollPromise,
                    enrollEndTime: common.dateFormat('yyyy-MM-dd HH:mm:ss', res.data.enrollEndTime),
                    lotteryTime: common.dateFormat('yyyy-MM-dd HH:mm:ss', res.data.lotteryTime),
                })
            } else {
                common.showToast(res.msg)
            }
        })
    },
    // 券的信息
    coupFn() {
        var that = this
        var temp = {
            activId: that.data.activId
        }
        common.post('/adminCouponCon/apps/bookingAdminCouponParams', temp, function (res) {
            if (res.status === 200) {
                console.log(res.data, "11111")
                that.setData({
                    textList: res.data
                })
            } else { }
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        const that = this
        common.getLocalUserInfo(function (success, data) {
            if (success) {
                console.log(data, "datadatadatadata")
                that.setData({
                    userInfo: data.user,
                    token: data.token
                }, function () {

                })
            } else {
                that.setData({
                    userInfo: null,
                    token: '',
                })
            }
        }, true)
    },

})